package com.sec.android.app.myfiles.module.search;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

public class SearchMenuImp extends AbsMenuImp {
    public SearchMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public boolean _onOptionsItemSelected(MenuItem item) {
        boolean handled = false;
        if (mFragment instanceof SearchFragment) {
            StorageType enteredStorage = ((SearchFragment) mFragment).getEnteredStorageType();
            boolean needOfflineCheck = (enteredStorage == StorageType.Cloud);

            if (!needOfflineCheck && enteredStorage == StorageType.Home) {
                ArrayList<FileRecord> selectedFile = mFragment.getSelectedFile();
                if (selectedFile != null && !selectedFile.isEmpty()) {
                    FileRecord record = selectedFile.get(0);
                    if (record != null) {
                        needOfflineCheck = (record.getStorageType() == StorageType.Cloud);
                    }
                }
            }

            if (needOfflineCheck) {
                int id = item.getItemId();
                boolean canWithoutNetwork = (id == R.id.menu_details);
                if (canWithoutNetwork || UiUtils.canExecuteCloud(mActivity, mFragment.getFragmentManager())) {
                    handled = _onSearchOptionsItemSelected(item);
                }
            } else {
                handled = _onSearchOptionsItemSelected(item);
            }
        } else {
            handled = super._onOptionsItemSelected(item);
        }
        return handled;
    }

    private boolean _onSearchOptionsItemSelected(MenuItem item) {
        boolean handled;
        if (item.getItemId() == R.id.menu_details) {
            SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.DETAILS, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
            MyFilesFacade.showDetail(mFragment.getProcessId(), mContext, mFragment, mFragment.getSelectedFile(), true);
            handled = true;
        } else {
            handled = super._onOptionsItemSelected(item);
        }
        return handled;
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);
        setMenuItemVisibility(menu, R.id.menu_toggle_list_type, AppFeatures.isTabletUIMode());
        setMenuItemVisibility(menu, R.id.menu_sync, false);
        setMenuItemVisibility(menu, R.id.menu_edit, false);
        setMenuItemVisibility(menu, R.id.menu_import, false);
        setMenuItemVisibility(menu, R.id.menu_share, false);
        setMenuItemVisibility(menu, R.id.menu_vzcloud, false);
        setMenuItemVisibility(menu, R.id.menu_create_folder, false);
        setMenuItemVisibility(menu, R.id.menu_sort_by, false);
        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, false);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_optimize_storage, false);
        setMenuItemVisibility(menu, R.id.menu_settings, true);
        setMenuItemVisibility(menu, R.id.menu_open, false);
        setMenuItemVisibility(menu, R.id.menu_open_in_a_new_window, false);
        setMenuItemVisibility(menu, R.id.menu_view_as, false);
    }

    @Override
    protected void setMenuItemIcon(MenuItem menuItem) {
        if (menuItem != null) {
            // In Search, only Detailed List is available so always set toggle icon as Grid
            menuItem.setIcon(mContext.getResources().getDrawable(R.drawable.action_bar_toggle_menu_grid_icon, mContext.getTheme()));
            menuItem.setTitle(mContext.getResources().getString(R.string.grid));
        }
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        super.onPrepareEditOptionsMenu(menu);

        StorageType enteredStorage = StorageType.None;
        if (mFragment instanceof SearchFragment) {
            enteredStorage = ((SearchFragment) mFragment).getEnteredStorageType();
        }

        setMenuItemVisibility(menu, R.id.menu_rename, false);

        ArrayList<FileRecord> selectedItem = mFragment.getSelectedFile();
        int selectedCount = selectedItem.size();

        if (selectedCount > 0 && isSharableRecords(selectedItem)) {
            setMenuItemVisibility(menu, R.id.menu_share, true);
        }

        if (enteredStorage == StorageType.Recent) {
            // In Recent files, The concept of list item is not real files,
            // and it's just a history. So copy/move doesn't provided.
            // And it's not a matter with cloud gateway is installed or not.
            setMenuItemVisibility(menu, R.id.menu_delete, false);
            setMenuItemVisibility(menu, R.id.menu_remove, false);
            setMenuItemVisibility(menu, R.id.menu_details, false);
            setMenuItemVisibility(menu, R.id.menu_copy, false);
            setMenuItemVisibility(menu, R.id.menu_move, false);

            if (selectedCount > 0) {
                setMenuItemVisibility(menu, R.id.menu_remove, true);
                setMenuItemVisibility(menu, R.id.menu_details, true);
                showMoreMenu(menu, R.id.menu_details, false);
            }
        }
    }

    @Override
    protected boolean showShareMenu() {
        boolean bRet = false;
        if (AppFeatures.isTabletUIMode() || mFragment.isSelectActionMode()) {
            bRet = super.showShareMenu();
        }

        return bRet;
    }

    @Override
    public void createContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {

    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        return false;
    }

    @Override
    protected boolean isSharableRecords(ArrayList<FileRecord> selectedItem) {
        boolean bRet = true;
        if (selectedItem != null) {
            for (FileRecord record : selectedItem) {
                if (!UiUtils.isCheckable(mContext, record, mNavigationInfo, true) ||
                        record.getStorageType() == StorageType.Cloud || !FileUtils.isSameAsRealFile(record) ||
                        !UiUtils.isShareAvailable(mContext, record)) {
                    bRet = false;
                    break;
                }
            }
        } else {
            bRet = false;
        }
        return bRet;
    }

}